package co.fitstart.mobile.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.entity.coupon.CreditCouponBatch;
import co.fitstart.entity.credit.Credit;
import co.fitstart.entity.credit.CreditTransaction;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CreditCouponBatchService;
/**
 * 
 * @author Ysy
 *
 */
@Service("creditCouponService")
public class CreditCouponBatchServiceImpl implements CreditCouponBatchService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.CreditCouponBatchService#listAll()
     */
    @SuppressWarnings("unchecked")
    public List<CreditCouponBatch> listAll() {
        return genericDaoSupport.searchForList("FROM CreditCouponBatch creditCouponBatch WHERE creditCouponBatch.amount > creditCouponBatch.releaseAmount AND (creditCouponBatch.effectiveEndDate > :today OR creditCouponBatch.effectiveEndDate IS NULL)", "today", new Date());
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.CreditCouponBatchService#load(java.lang.Long)
     */
    public CreditCouponBatch load(Long id) {
        return genericDaoSupport.searchForObject("FROM CreditCouponBatch creditCouponBatch where creditCouponBatch.id = :id", "id", id, CreditCouponBatch.class);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.CreditCouponBatchService#exchange(co.fitstart.entity.credit.Credit, co.fitstart.entity.coupon.CreditCouponBatch)
     */
    public void exchange(Credit credit, CreditCouponBatch creditCouponBatch) {
        
        // validate exchange first
        this.validateExchange(credit, creditCouponBatch);
        
        User user = credit.getUser();
        
        //1.eidt CouponBatch、CreditCoupon's claimed amount & release amount
        CouponBatch couponBatch = creditCouponBatch.getCouponBatch();
        
        genericDaoSupport.update(couponBatch.editClaimedAmount().editReleaseAmount(1));
        genericDaoSupport.update(creditCouponBatch.editClaimedAmount().editReleaseAmount(1));
        
        //2.create a new Coupon
        Long userId = user.getId();
        Coupon coupon = new Coupon(couponBatch);
        genericDaoSupport.save(coupon.bindUser(userId));
        
        //3.edit credit's credit value
        int usedCredit = creditCouponBatch.getCreditValue();
        genericDaoSupport.update(credit.useCredit(usedCredit));
        
        //4.create a new creditTransaction
        CreditTransaction creditTransaction = new CreditTransaction(credit);
        genericDaoSupport.save(creditTransaction.exchangeCoupon(creditCouponBatch));
    }
    
    /**
     * Validate credit coupon batch here
     * 
     * @param credit
     * @param creditCouponBatch
     */
    protected void validateExchange(Credit credit, CreditCouponBatch creditCouponBatch) {
        
        if (creditCouponBatch == null) {
            throw new BusinessException("认领参数异常");
        }
        
        if (creditCouponBatch.getRestAmount() <= 0) {
            throw new BusinessException("兑换失败：该积分券已被兑换完");
        }
        
        if (!creditCouponBatch.isEffective()) {
            throw new BusinessException("兑换失败：该积分券已失效");
        }
        
        if (credit == null || credit.getAvailableCredit() < creditCouponBatch.getCreditValue()) {
            throw new BusinessException("兑换失败：您的积分余额已不足");
        }
    }

}
